package 多线程.多线程创建.线程安全.死锁;

public class MyTicket implements Runnable {
    private static Object lockA = new Object();
    private static Object lockB = new Object();

    private boolean flag;

    public MyTicket(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        if (flag) {
            synchronized (lockA) {
                System.out.println("线程" + Thread.currentThread().getName() + "获取了锁A");
                synchronized (lockB) {
                    System.out.println("线程" + Thread.currentThread().getName() + "获取了锁B");
                }
            }
        } else {
            synchronized (lockB) {
                System.out.println("线程" + Thread.currentThread().getName() + "获取了锁B");
                synchronized (lockA) {
                    System.out.println("线程" + Thread.currentThread().getName() + "获取了锁A");
                }
            }
        }

    }
}
